#!/bin/sh

set -eo pipefail

code_path=$(pwd)

# docker run --tty will merge stderr and stdout, we don't need this on CI or
# it will break codequality json file
[ "$CI" != "" ] || docker_tty="--tty"

# Preparing gocyclo analyzer
docker pull registry.gitlab.com/nolith/codeclimate-gocyclo > /dev/null
docker tag registry.gitlab.com/nolith/codeclimate-gocyclo codeclimate/codeclimate-gocyclo > /dev/null

# Preparing govet analyzer
#
# Current version of govet analyzer is compiled with Go 1.13 and forces usage
# of Go Modules. This unfortunatel doesn't work properly if any external dependencies
# are used: https://github.com/codeclimate-community/codeclimate-govet/issues/25.
# Bellow we're pulling custom version of govet that should work properly.
# When the problem on the `codeclimate-govet` analyzer will be resolved, we should
# remove this override and start using the default, latest version again.
docker pull registry.gitlab.com/tmaczukin/codeclimate-govet:tm-1.13.1-alpine3.10-2 > /dev/null
docker tag registry.gitlab.com/tmaczukin/codeclimate-govet:tm-1.13.1-alpine3.10-2 codeclimate/codeclimate-govet:latest > /dev/null

# Executing codeclimate check
exec docker run --rm $docker_tty \
	--env CODECLIMATE_CODE="$code_path" \
	--volume "$code_path":/code \
	--volume /var/run/docker.sock:/var/run/docker.sock \
	--volume /tmp/cc:/tmp/cc \
	codeclimate/codeclimate:0.83.0 "$@"
